package dao.impl;

import dao.BaseDao;
import dao.CheckItemDao;
import entity.CheckItem;
import utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: Hospital
 * @package: dao.impl
 * @className: CheckItemDaoImpl
 * @author: 翟
 * @date: 2025/3/31 11:56
 */

public class CheckItemDaoImpl extends BaseDao implements CheckItemDao {

    public CheckItemDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public int addCheckItemInfo(CheckItem checkItem) throws SQLException {
        String sql = "insert into checkitem (checkItemName , checkItemCost) values (? , ?)";
        return executeUpdate(sql, checkItem.getCheckItemName(), checkItem.getCheckItemCost());
    }

    @Override
    public int updateCheckItemCostByCheckItemName(CheckItem checkItem) throws SQLException {
        StringBuffer stringBuffer = new StringBuffer("update `checkItem` set `checkItemCost` = ? ");
        stringBuffer.append("where `checkItemName` = ? ");
        return executeUpdate(stringBuffer.toString() , checkItem.getCheckItemCost() , checkItem.getCheckItemName());
    }

    @Override
    public List<CheckItem> getAll() throws SQLException{
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery("select * from `checkItem`");
            List<CheckItem> list = new ArrayList<>();
            while (resultSet.next()){
                CheckItem checkItem = new CheckItem();
                checkItem.setCheckItemId(resultSet.getInt(1));
                checkItem.setCheckItemName(resultSet.getString(2));
                checkItem.setCheckItemCost(resultSet.getDouble(3));
                list.add(checkItem);
            }
            return list;
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
    }
}
